package com.goertek.checklist.controller;

import com.goertek.checklist.domain.Checklist;
import com.goertek.checklist.service.IChecklistService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import javafx.scene.control.CheckBox;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * checklist模块 Controller
 * @author zhixuebin
 *
 * @date 2022-04-27
 */

@RestController
@RequestMapping("/goertek/checklist")
public class CheckListController extends BaseController {

    @Autowired
    IChecklistService checklistService;

    /**
     * 新增记录
     * @param checkList
     * @return
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Checklist checkList){

        //通过编码查询去重校验
        Checklist result = checklistService.getChecklistItem(checkList.getCode());

        int i = 0;
        if(StringUtils.isNull(result)){
            Checklist list = new Checklist();
            list.setId(checkList.getId());
            list.setCode(checkList.getCode());
            list.setLabel(checkList.getLabel());
            list.setDescription(checkList.getDescription());

            i = checklistService.insertChecklist(list);
        }

        if(i > 0) {
            return AjaxResult.success("添加成功");
        }else {
            return AjaxResult.error("添加失败");
        }
    }

    /**
     * 查询列表
     * @param checklist
     * @return
     */
    @GetMapping("/getList")
    public TableDataInfo getList(Checklist checklist){
        startPage();
        List<Checklist> list = checklistService.getList(checklist);
        return getDataTable(list);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids){
        return toAjax(checklistService.deleteChecklist(ids));
    }

    /**
     * 逻辑删除
     * @param ids
     * @return
     */
    @DeleteMapping("/delete/{ids}")
    public AjaxResult remove2(@PathVariable String[] ids){
        int i = 0;
        for(String id : ids){
            Checklist list = checklistService.getChecklistItem(id);
            list.setDelStatus(1);
            checklistService.updateChecklist(list);
            i++;
        }
        return AjaxResult.success("已删除"+ i +"条记录");
    }

    /**
     * 编辑修改
     * @param checklist
     * @return
     */
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody Checklist checklist){
        //去重验证
        Checklist list = new Checklist();
        list.setId(checklist.getId());
        list.setCode(checklist.getCode());
        list.setTerritory(checklist.getTerritory());
        list.setProductLine(checklist.getProductLine());
        list.setApplySituation(checklist.getApplySituation());
        list.setDescription(checklist.getDescription());
        list.setLabel(checklist.getLabel());
        List<Checklist> l = checklistService.getList(list);

        if (StringUtils.isNull(l) || l.size() < 1) {
            return toAjax(checklistService.updateChecklist(list));
        }else{
            return AjaxResult.error("编辑失败");
        }
    }

}
